package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Business;
import com.qk.entity.Clue;
import com.qk.entity.ClueTrackRecord;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.BusinessService;
import com.qk.service.ClueService;
import com.qk.utils.CurrentUserHoler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessService businessService;

    @Override
    public PageResult<Clue> querylist(ClueDto clueDto) {
        Page<Clue> page =clueMapper.query(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
    return new PageResult<>(page.getTotal(),page.getRecords());
    }

    @Override
    public Clue queryById(Integer id) {
        return clueMapper.selectId(id);
    }

    @Override
    @Transactional
    public void updateClue(Clue clue) {
        log.info("更新线索{}",clue);
        clue.setStatus(3);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);


        //修改线索跟踪记录
        ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
        clueTrackRecord.setClueId(clue.getId());
        //从当前用户获取
        clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        clueTrackRecord.setSubject(clue.getSubject());
        clueTrackRecord.setLevel(clue.getLevel());
        clueTrackRecord.setRecord(clue.getRecord());
        clueTrackRecord.setNextTime(clue.getNextTime());
        clueTrackRecord.setType(1);  //线索跟踪跟进中
        clueTrackRecord.setCreateTime(LocalDateTime.now());
        clueTrackRecordMapper.insert(clueTrackRecord);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFalseClue(Integer id, MarkFalseClueDto markFalseClueDto) {
        log.info("修改线索{}{}",id,markFalseClueDto);
        Clue clue = new Clue();
        clue.setId(id);
        clue.setStatus(4);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
            //修改线索跟踪记录
            ClueTrackRecord clueTrackRecord = new ClueTrackRecord();
            clueTrackRecord.setClueId(clue.getId());
            clueTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
            clueTrackRecord.setType(0);
            clueTrackRecord.setFalseReason(markFalseClueDto.getReason());
            clueTrackRecord.setRecord(markFalseClueDto.getRemark());
            clueTrackRecord.setCreateTime(LocalDateTime.now());
            clueTrackRecordMapper.insert(clueTrackRecord);

        }

    /**
     * 线索转商机
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void toBusiness(Integer id) {
        log.info("修改线索{}为商机",id);
        Clue clue = clueMapper.selectById(id);
        clue.setStatus(5);
        clue.setUpdateTime(LocalDateTime.now());
        //更新线索
        clueMapper.updateById(clue);
        //添加商机
        Business business = new Business();
        BeanUtils.copyProperties(clue,business);
        business.setId(null);
        business.setUserId(null);
        business.setStatus(1);
        business.setClueId( id);
        business.setNextTime(null);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
    }
    /**
     * 查询线索池
     */
    @Override
    public PageResult<Clue> querylistPool(ClueDto clueDto) {
         Page<Clue> page =clueMapper.queryPool(new Page<Clue>(clueDto.getPage(), clueDto.getPageSize()), clueDto);
        return new  PageResult<>(page.getTotal(),page.getRecords());
    }
}

